<?php
/**
 * Created by PhpStorm.
 * User: 27598
 * Date: 2019/6/20
 * Time: 14:25
 * 整体逻辑是这样的：（租金+基础服务费+手续费）* 天数 + 小时数*日租金+1*(基础服务费+手续费)
 */

namespace App\Helpers;
use App\Models\BaseConfig;
use App\Models\Car;
use App\Models\CarCalendar;
use App\Models\CarService;
use App\Models\ConfigTable;
use App\Models\Coupon;
use App\Models\CouponLog;
use App\Models\HoursPrice;
use App\Models\LeaseDaily;
use App\Models\User;
use Illuminate\Support\Carbon;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\ForbidLeaseDate;
use App\Models\Car\ActivePrice;
trait NewBaseData
{
    public $hours                = 0;
    public $seconds              = 0;
    public $brokerage            = 0;//手续费
    public $base_price           = 0;//基础服务费
    public $lease_price          = 0;//租金
    public $car_service          = 0;//车精选服务
    public $daily                = 0;//一共多少天
    public $daily_zj                = 0;//计算日租金的一共多少天
    public $daily_cj                = 0;//春节一共多少天
    public $total                = 0;
    public $all_hours            = 0;
    public $car;
    public $rate                 = 1; // 其他区间的总折扣系数
    public $rate_cj                 = 1; // 春节期间的总折扣系数
    public $additional = 1; // 折上折(特指春节的单独折扣)
    public $daily_price          = 0.00;//当日租金
    public $daily_price_cj          = 0.00;//春节当日租金
    public $daily_price_jj          = 0.00;//春节期间日租金加价金额
    public $daily_rate          = 0;//春节期间打折覆盖春节价格的系数
    public $coupon = null;
    public $deposit = -1;
    public $req=null;
    public $days = 0;
    public $vip_service = 0.00; //尊享服务
    public $worry_fee = 0.00;//车行无忧
    public $paint_finish = 0.00;//漆面无忧

    public $all_vip_service = 0.00; //尊享服务
    public $all_worry_fee = 0.00;//车行无忧
    public $all_paint_finish = 0.00;//漆面无忧
    public $pre_base_price_total = 0.00; // 基本保障费
    public $pre_brokerage_total = 0.00; // 车辆整备费
    public $pre_service_total = 0.00; // 精选服务
    public $hour_price = 0;
    public $yxhdrate = 1; // 营销活动折扣
    public $yxhdzsz = 2; // 营销活动折上折1是2否
    // 设置
    public function setPreServiceTotal($price){
        $this->pre_service_total = $price;
        return $this;
    }
    // 获取
    public function getPreServiceTotal(){
        return $this->pre_service_total;
    }
    public function orderService($str, $diffDays, $car)
    {
        $arr = explode(',', $str);
        $service = CarService::whereIn('id', $arr)->get();
        $data = [];
        foreach ($service as $k => $item) {
            $map = [
                'car_id' => $car->id,
                'name' => $item->name,
                'total_price' => $this->checkServicePrice($item->price),
                'days' => $diffDays,
                'price' => $item->price,
                'introduction' => $item->introduction,
            ];
            array_push($data, $map);
        }
        return $data;
    }
    public function checkServicePrice($price)
    {
        $data = $price * ($this->daily+$this->daily_cj);
        if ($this->all_hours < 24) {
            $data = $price;
        }else {
            if ($this->hours != -1 && $this->hours != 0) {
                $data = $this->floatPrice($price * ($this->daily+$this->daily_cj) + $price / 2);
            }
        }

        return $data;
    }
    public function SenvenDay($price)
    {
        $data = [];
        for ($i = 0; $i < 7; $i++) {
            array_push($data, $this->baseData($i, $price));
        }

        return $data;
    }
    public function baseData(int $num, $price)
    {
        return [
            'day' => $this->getDay($num),
            'week' => '周'.$this->getWeek($num),
            'day_price' => $price
        ];
    }
    public function getDay(int $num)
    {
        return now()->addDay($num)->format('Ymd');
    }
    public function getWeek(int $num)
    {
        return $this->formatToWeek(now()->addDay($num)->weekday());
    }
    public function formatToWeek(int $num)
    {

        if ($num == 1) {
            return '一';
        }
        if ($num == 2) {
            return '二';
        }
        if ($num == 3) {
            return '三';
        }
        if ($num == 4) {
            return '四';
        }
        if ($num == 5) {
            return '五';
        }
        if ($num == 6) {
            return '六';
        }
        if ($num == 0) {
            return '日';
        }
    }
    //格式化当前日期
    public function getDate($time, $day = 0)
    {
        return '周'.$this->formatToWeek(Carbon::parse($time)->addDay($day)->weekday());
    }
    public function getCarlendarPrice($car_id)
    {
        // info('车辆信息'.$car_id->id);
        return CarCalendar::query()->where('car_id', $car_id->id)->where('type',2)->pluck('day_price', 'week');
    }
    public function getPrice($car, $days)
    {
        $this->daily_price = $car->minimum_price+$this->daily_price;
        $this->daily_price_cj = $car->minimum_price+$this->daily_price_jj;
        $price = $car->minimum_price * $days;
//        $price = 0;
//        $data = $this->getCarlendarPrice($car);
//        // info('车辆信息'.json_encode($car));
//        // info('获取周折扣'.json_encode($data));
//        // info('获取天数'.$day);
//        // info('获取总天数'.$days);
//        if (count($data) > 0) {
//            for ($i = 0; $i < $days; $i++) {
//                $week = $this->getDate($day, $i);
//                if ($data[$week]) {
//                    $this->daily_price = $data[$week]+$this->daily_price;
//                    $price = $price + $data[$week];
//                }else {
//                    $this->daily_price = $car->minimum_price+$this->daily_price;
//                    $price = $price + $car->minimum_price;
//                }
//            }
//        }else {
//            $this->daily_price = $car->minimum_price+$this->daily_price;
//            $price = $car->minimum_price * $days;
//        }
        return $price;
    }
    public function diffSeconds($end, $sta)
    {
        return strtotime($end) - strtotime($sta);
    }
    public function diffHours($request)
    {
        return Carbon::parse($request->pre_fetch_time)->diffInHours(Carbon::parse($request->pre_return_time));
    }
    //当日小时数
    public function lastHours($start,$end){
        $second = $this->diffSeconds($end, $start);
        $days = Carbon::parse($start)->diffInDays(Carbon::parse($end));
        if($second<3600 * 6){
            return 0;
        }else{
            $day = 3600 * 24 * $days;
            $second = $second - $day;
            if ($second >= 3600 * 6) {
                return  -1;
            }else {
                $last_time = ceil($second / 3600);
                return  $last_time;
            }
        }
    }
    public function diffDays($start, $end)
    {
        $days = Carbon::parse($start)->diffInDays(Carbon::parse($end));
        $second = $this->diffSeconds($end, $start);
        if($second<3600 * 6){
            $this->days = 1;
            $this->hours = 0;
        }else{
            $day = 3600 * 24 * $days;
            $second = $second - $day;
            if ($second >= 3600 * 6) {
                $this->days = $days + 1;
                $this->hours = -1;
            }else {
                $this->days = $days;
                $last_time = ceil($second / 3600);
                $this->hours = $last_time;
            }
            return $this->days;
        }

    }
    public function getLimitSeconds()
    {
        return BaseConfig::find(1, 'limit_seconds');
    }
    public function user($openid)
    {
        return User::where('openid', $openid)->first();
    }
    public function seriesCar($car_id)
    {
        return Car::query()->find($car_id);
    }
    //优惠券返还
    public function recoupon($coupon_id)
    {
        $data = CouponLog::find($coupon_id);
        $data->status = 0;
        $data->save();
    }
    public function get_coupon($coupon_id)
    {
        return Coupon::find($coupon_id);
    }
    //获取天数比例系数
    public function getRate()
    {
        if($this->rate ==1){
            $data = LeaseDaily::where('day', '<=', $this->daily)->min('rate');
            if($data){
                return $this->rate = $data;
            }
        }else{
            return $this->rate;
        }
    }
    //当3天的折扣
    public function threeRate()
    {
        return LeaseDaily::where('day', 3)->value('rate');
    }
    //分类计费
    public function getCategoryPrice($category)
    {
        $this->hour_price = HoursPrice::where('category', $category)->value('price');
        return $this->hour_price;
    }
    //按时计费
    public function hourPrice($price)
    {
        return $hour_price = $this->floatPrice($price / 2);
    }
    //保留两位小数的价格
    public function floatPrice($str)
    {
        $resArr = explode('.', $str);
        if (isset($resArr[1])) {
            return $resArr[0].'.'.substr($resArr[1], 0, 2);
        }

        return $str;
    }

    public function factory($request)
    {
        //基本保障费
        $this->base_price = $this->car->base_price;
        //车辆整备费
        $this->brokerage = $this->car->brokerage;
        //精选服务
        $this->car_service = $this->services($request->service);
        //当前租金
        $this->lease_price = $this->getPrice($this->car, $this->daily);
        // info('获取租金'.$this->lease_price);
    }


    public function services($str)
    {
        $arr = explode(',', $str);

        return $this->getService($arr);
    }
    public function getService($arr)
    {
        $service =  CarService::whereIn('id', $arr)->get();
        $price = 0.00;
//        public $vip_service = 0.00; //尊享服务
//        public $worry_fee = 0.00;//车行无忧
//        public $paint_finish = 0.00;//漆面无忧
        if($service){
            foreach ($service as $v){
                $price+=$v->price;
                if($v->id==1){
                    $this->paint_finish = $v->price;
                }
                if($v->id==2){
                    $this->worry_fee = $v->price;
                }
                if($v->id==3){
                    $this->vip_service = $v->price;
                }
            }
        }
        return $price;

    }
    public function ltOneDay($request)
    {
        if($request->filled('yz')){
            $data = $this->basePrice(1,0.6);
        }else{
            $data = $this->basePrice(1,1);
        }
        return $data;
    }

    //小于3天没有折扣
    public function ltThreeDays()
    {
        $data = $this->getAllPrice($this->daily,1);
        return $data;
    }

    //大于3天有折扣
    public function gtThreeDays($rate)
    {
        if ($this->rate_cj==1) {
            $data = $this->getAllPrice($this->daily,$rate);
        } else {
            $data = $this->getAllPrice($this->daily,$this->rate_cj);
        }
        return $data;
    }
    // 获取春节活动配置
    protected function getCjConfig($request){
        return ActivePrice::query()->select('rate','days','additional')->where('type',$request->cj)->get();
    }
    // 区分打折和不打折
    protected function diffDiscount($map){
//        $isDiscont = [];
        $unDiscont = [];
        // foreach ($map as $item){
        //     if($item->rate==1){
        //         $isDiscont = $item;
        //     }else{
        //         $unDiscont[] = $item;
        //     }
        // }
        $isDiscont = $map[0];
        $unDiscont[0] = $map[1];
        $unDiscont[1] = $map[2];
        return [$isDiscont,$unDiscont];
    }
    // 获取总价运算
    /**
     * 春节期间整体逻辑
     * 只要租车时间涉及春节设定的时间，春节设定了几天，订单必须满足几天;
     * 春节前后+春节小于20天，春节前后相加使用正常折扣+春节期间不打折;
     * 春节前后+春节大于等于20天小于30天，春节前后相加使用正常折扣+春节0.9折
     * 春节前后+春节大于等于30天小于45天，最低日租金*租车天数*0.9(覆盖春节价格)
     * 春节前后+春节大于等于45，最低日租金*租车天数*0.8(覆盖春节价格)
     * 后台可按车型设置加价金额为春节日租金;
     * 用户小程序的涉及变更:如果选择时间段包括春节，那么提示:所租时间涉及春节，租车时长至少需要满足春节10天;
     */
    public function totalPrice($request)
    {
        //获取天数
        $this->daily = $this->diffDays($request->pre_fetch_time, $request->pre_return_time);
        $this->yxhdrate = $request->yxhdrate??1;
        $this->yxhdzsz = $request->yxhdzsz??2;
        // 调用春节价格
        if($request->filled('cj')&&$request->cj == 1){
            // 春节天数
            $this->daily_cj = $request->cjdays;
            $this->cjExtra();
            // 如果当前租期大于20天
            $cjConfig = $this->getCjConfig($request);
            list($a,$b) = $this->diffDiscount($cjConfig);
            // info($a);
            // info($b);
            $bres = array_reverse($b);
            // 当前租期大于优惠条件时候 大于等于20天
            $this->additional = $a->rate;
            if($this->daily>=$a->days){
                // 设置折上折
                // 其它情况走春节活动价格
                foreach ($bres as $item){
                    if($this->daily>=$item->days){
                        $this->rate_cj = $item->rate;
                        break;
                    }
                }
            }
            $this->daily_zj = $this->daily - $this->daily_cj;
            // 如果天数是大于配置的第二项的，覆盖春节价格
            if ($this->daily >= $bres[1]['days']) {
                // info('如果天数是大于配置的第二项的，覆盖春节价格');
                $this->daily_cj = 0;
                $this->daily_zj = $this->daily;
            }
        } else {
            $this->daily_cj = 0;
            $this->daily_zj = $this->daily;
        }
        $this->daily = $this->daily_zj;
        // info('订单一共多少天：'.$this->daily);
        // info('春节一共多少天：'.$this->daily_cj);
        // info('租金折扣需要计算的一共多少天：'.$this->daily_zj);

        //获取基础数据
        $this->factory($request);
        // info('春节加价后的日租金'.$this->daily_price_cj);
        //获取小时数
        $this->all_hours = $this->diffHours($request);

        //小于24小时按照1天来计算
        if ($this->all_hours < 24) {
            $data = $this->ltOneDay($request);
        }else {
            if ($this->daily == 2 && $this->hours >= 6) {
                $this->rate = $rate = $this->threeRate();
                $data = $this->gtThreeDays($rate);
            }else{
                //获取费率
                $this->getRate();
                if ($this->rate) {
                    $data = $this->gtThreeDays($this->rate);
                }else {
                    $data = $this->ltThreeDays();
                }
            }
        }
        // info('获取当前费率'.$this->rate);
        // info('获取春节当前费率'.$this->rate_cj);
        // info('春节天数折扣费率：'.$this->additional);

        //是否使用优惠券
        if ($request->coupon==1) {
            $this->coupon = $this->is_use_coupon($request);
            if($this->coupon){
                $data['bc_total'] = $this->floatPrice($data['bc_total'] - $this->coupon->price);
                $data['coupon_price'] = $this->coupon->price;
            }
        }
        $data['rate'] = $this->rate;
        $data['rate_cj'] = $this->rate_cj;
        $data['daily_rate'] = $this->daily_rate;
        $data['hours'] = $this->hours;
        $data['hours_price'] = $this->hour_price;
        info('订单一共多少天：'.$this->daily.'，春节一共多少天：'.$this->daily_cj.'，租金折扣需要计算的一共多少天：'.$this->daily_zj.'，春节加价后的日租金：'.$this->daily_price_cj.'，获取当前费率：'.$this->rate.'，获取春节当前费率：'.$this->rate_cj.'，春节天数折扣费率：'.$this->additional);
        // 金额不能小于0
        if($data['bc_total']<=0){
            $data['bc_total'] = '0.01';
        }
        return $data;
    }
    //获取优惠券
    public function getCoupon($openid){
        return CouponLog::where('openid', $openid)->first();
    }
    //查询改用户是否拥有未使用优惠券
    public function coupon($openid)
    {
        return CouponLog::where('openid', $openid)->where('status', 0)->orderBy('price','desc')->first();
    }
    public function is_use_coupon($request){
        return CouponLog::query()
            ->where('openid',$request->openid)
            ->where('status',0)->orderBy('price','desc')->first();
    }

    //获取押金功能
    protected function getDeposit(){
        if($this->req){
            if($this->req->deposit_used==2){
                $dep = 0;
            }else{
                if($this->req->deposit>=0){
                    $dep = $this->req->deposit;
                }else{
                    $dep = $this->car->deposit;
                }
            }
            if($this->req->break_rules_used==2){
                $bre = 0;
            }else{
                if($this->req->break_rules>=0){
                    $bre = $this->req->break_rules;
                }else{
                    $bre = $this->car->break_rules;
                }
            }
            $deposit = $dep+$bre;
        }else{
            $deposit = $this->car->deposit+$this->car->break_rules;//押金费用
        }
        return $deposit;
    }
    public function getAllPrice($days=1,$rate=1){
        if($this->hours==-1){
            // $days++;
            $data = $this->basePrice($days,$rate);
        }else{

            $data = $this->basePrice($days,$rate);

            if($this->hours>0){

                $deposit = $this->getDeposit();

                // $data['pre_base_price_total'] += $this->base_price;//基本保障费

                // $data['pre_brokerage_total'] += $this->brokerage;//车辆整备费
                $data['pre_service_total'] += $this->car_service/2;//精选服务
                $price = $this->getCategoryPrice($this->car->category) * $this->hours;
                $data['pre_lease_price_total'] += $price;
                $data['pre_total'] =$data['pre_service_total'] + $data['pre_base_price_total']+$data['pre_brokerage_total']+$data['pre_lease_price_total']+$deposit;

                // $data['bc_base_price_total'] += $this->base_price;
                // $data['bc_brokerage_total'] += $this->brokerage;
                $data['bc_service_total'] += $this->car_service/2;//精选服务
                // $price = $price*$rate;2024-12-27  13:15:45，小时价不计算折扣
                $data['bc_lease_price_total'] += $price;
                $data['bc_total'] = $data['pre_service_total'] + $data['bc_base_price_total']+$data['bc_brokerage_total']+$data['bc_lease_price_total']+$deposit;

                $this->all_vip_service+=$this->vip_service/2;
                $this->all_worry_fee+=$this->worry_fee/2;
                $this->all_paint_finish+=$this->paint_finish/2;
            }
        }

        return $data;
    }
    public function basePrice($days,$rate){
        $deposit = $this->getDeposit();//押金费用
        // 春节租金适配
        if ($this->daily_cj>0) {
            info('走春节租金适配');
            $data['base_price'] = $this->base_price;
            $data['base_days'] = $days;
            $data['pre_base_price_total'] = $this->base_price*$days + $this->base_price*$this->daily_cj;
            $data['pre_brokerage_total'] = $this->brokerage*$days + $this->brokerage*$this->daily_cj;
            $data['pre_lease_price_total'] = $this->lease_price + $this->daily_price_cj*$this->daily_cj;
            $data['pre_service_total'] = $this->car_service * floor($days+$this->daily_cj);
            $data['pre_total'] = $this->floatPrice($data['pre_service_total'] + $data['pre_base_price_total']+$data['pre_brokerage_total']+$data['pre_lease_price_total']+$deposit);

            if($this->yxhdrate != 1){
                if($this->yxhdzsz == 1){
                    $data['bc_base_price_total'] = $this->base_price * $days*$rate*$this->yxhdrate + $this->base_price*$this->daily_cj*$this->additional;//基本保障费
                    $data['bc_brokerage_total'] = $this->brokerage * $days*$rate*$this->yxhdrate + $this->brokerage*$this->daily_cj*$this->additional;//车辆整备费
                    $data['bc_lease_price_total'] = $this->lease_price*$rate*$this->yxhdrate + $this->daily_price_cj*$this->daily_cj*$this->additional;
                    $data['bc_service_total'] = $this->car_service * floor($days+$this->daily_cj);
                }else{
                    $data['bc_base_price_total'] = $this->base_price * $days*$this->yxhdrate + $this->base_price*$this->daily_cj*$this->additional;//基本保障费
                    $data['bc_brokerage_total'] = $this->brokerage * $days*$this->yxhdrate + $this->brokerage*$this->daily_cj*$this->additional;//车辆整备费
                    $data['bc_lease_price_total'] = $this->lease_price*$this->yxhdrate + $this->daily_price_cj*$this->daily_cj*$this->additional;
                    $data['bc_service_total'] = $this->car_service * floor($days+$this->daily_cj);

                }
            }else{
                $data['bc_base_price_total'] = $this->base_price * $days*$rate + $this->base_price*$this->daily_cj*$this->additional;//基本保障费
                $data['bc_brokerage_total'] = $this->brokerage * $days*$rate + $this->brokerage*$this->daily_cj*$this->additional;//车辆整备费
                $data['bc_lease_price_total'] = $this->lease_price*$rate + $this->daily_price_cj*$this->daily_cj*$this->additional;
                $data['bc_service_total'] = $this->car_service * floor($days+$this->daily_cj);
            }

            $this->all_vip_service=$this->vip_service*floor($days+$this->daily_cj);
            $this->all_worry_fee=$this->worry_fee*floor($days+$this->daily_cj);
            $this->all_paint_finish=$this->paint_finish*floor($days+$this->daily_cj);
        } else {
            $data['base_price'] = $this->base_price;
            $data['base_days'] = $days;
            $data['pre_base_price_total'] = $this->base_price*$days;
            $data['pre_brokerage_total'] = $this->brokerage * $days;
            $data['pre_lease_price_total'] = $this->lease_price;
            $data['pre_service_total'] = $this->car_service * floor($days);
            $data['pre_total'] = $this->floatPrice($data['pre_service_total'] + $data['pre_base_price_total']+$data['pre_brokerage_total']+$data['pre_lease_price_total']+$deposit);

            if($this->yxhdrate != 1){
                if($this->yxhdzsz == 1){
                    $data['bc_base_price_total'] = $this->base_price * $days*$rate*$this->yxhdrate;//基本保障费
                    $data['bc_brokerage_total'] = $this->brokerage * $days*$rate*$this->yxhdrate;//车辆整备费
                    $data['bc_lease_price_total'] = $this->lease_price*$rate*$this->yxhdrate;
                    $data['bc_service_total'] = $this->car_service * $days;
                }else{
                    $data['bc_base_price_total'] = $this->base_price * $days*$this->yxhdrate;//基本保障费
                    $data['bc_brokerage_total'] = $this->brokerage * $days*$this->yxhdrate;//车辆整备费
                    $data['bc_lease_price_total'] = $this->lease_price*$this->yxhdrate;
                    $data['bc_service_total'] = $this->car_service * $days;

                }
            }else{
                $data['bc_base_price_total'] = $this->base_price * $days*$rate;//基本保障费
                $data['bc_brokerage_total'] = $this->brokerage * $days*$rate;//车辆整备费
                $data['bc_lease_price_total'] = $this->lease_price*$rate;
                $data['bc_service_total'] = $this->car_service * $days;
            }

            $this->all_vip_service=$this->vip_service*$days;
            $this->all_worry_fee=$this->worry_fee*$days;
            $this->all_paint_finish=$this->paint_finish*$days;
        }
        $data['bc_total'] = $this->floatPrice($data['bc_service_total']+$data['bc_base_price_total']+$data['bc_brokerage_total']+$data['bc_lease_price_total']+$deposit);
        return $data;
    }
    //被禁止排车车辆
    protected function getForbitDateCar($start,$end){
        $array = [$start,$end];
        $data =  ForbidLeaseDate::query()
            ->whereIn('forbid_date',$array)
            ->pluck('car_id')->toArray();
        if(count($data)>0){
            return array_unique($data);//移除重复
        }
    }
    protected function getNewOrderModel(){
        return CarOrder::query()
            ->with('user','gear','customer','car',"customer");
    }
    protected function searchOrderModel($request){
        $model = $this->orderWith();
        if($request->filled('source')){
            $model = $model->where('source',$request->source);
        }
        if($request->filled('search')){
            $search = $request->search;
            $model = $model->where(function($query)use($search){
                $query->where('phone','like','%'.$search.'%')
                    ->orWhere('wx_name','like','%'.$search.'%')
                    ->orWhere('source','like','%'.$search.'%')
                    ->orWhere('department','like'.'%'.$search.'%')
                    ->orWhere('nickname','like','%'.$search.'%')
                    ->orWhere('customer_name','like','%'.$search.'%')
                    ->orWhere('name','like','%'.$search.'%')
                    ->orWhere('plate_number','like','%'.$search.'%')
                    ->orWhere('license_number','like','%'.$search.'%')
                    ->orWhere('order_sn',$search);
            });
        }
        return $model;
    }
    protected function computeBcTotal($request,$data){
        if($request->filled('get_true_deport')&&$request->get_true_deport>0){
            $deposit = $request->get_true_deport;
        }else{
            //押金
            if($request->filled('deposit_used')&&$request->deposit_used==2){
                $deposit = 0;
            }else{
                $deposit = $this->car->deposit;
            }
        }
        if($request->filled('violation_amount')&&$request->violation_amount>0){
            $break_rules = $request->violation_amount;
        }else{
            if($request->filled('break_rules_used')&&$request->break_rules_used==2){
                $break_rules = 0;
            }else{
                $break_rules = $this->car->break_rules;
            }
        }


        //总押金
        $all_deposit = $deposit+$break_rules;
        $acc_total = $data['bc_service_total']+$data['bc_base_price_total']+$data['bc_brokerage_total']+$data['bc_lease_price_total'];
        $bc_total = $acc_total+$all_deposit;
        // info('未使用优惠券之前的bc_total'.$bc_total);
        if($this->coupon&&$request->filled('coupon')&&$request->coupon>0){
            $data['is_coupon'] = 1;//启用优惠券
            $data['coupon_id'] = $this->coupon->id;
            $data['coupon_price'] = $this->coupon->price;
            $this->coupon->status = 1;
            $this->coupon->save();
            $bc_total = $bc_total-$this->coupon->price;
            // info('优惠券价格'.$this->coupon->price);
        }
        //设置优惠券
//        if($this->coupon&&$request->filled('coupon')&&$request->coupon>0){
//            $bc_total = $this->getCoupon($bc_total);
//        }
        // 金额不能小于0
        if($bc_total<=0){
            $bc_total = '0.01';
        }
        return [$bc_total,$deposit,$break_rules];
    }
    //获取orderWith
    protected function orderWith(){
        return CarOrder::query()
            ->with('user','gear','customer','car','initCar','wxCustomer','phoneCustomer','contracts', 'service');
    }
    //计算春节基础价格
    protected function cjExtra(){
        // 获取春节价格
        $cj = ConfigTable::query()->where('key','cjConfig')->value('value');
        if($cj){
            $arr = json_decode($cj,true);
            switch ($this->car->category){
                case '新能源':
                    $this->daily_price_jj = $arr['xny'];
                    break;
                case '经济型':
                    $this->daily_price_jj = $arr['jjx'];
                    break;
                case '舒适型':
                    $this->daily_price_jj = $arr['ssx'];
                    break;
                case '豪华型':
                    $this->daily_price_jj = $arr['hhx'];
                    break;
                case '商务型':
                    $this->daily_price_jj = $arr['swx'];
                    break;
                case '越野型':
                    $this->daily_price_jj = $arr['yyx'];
                    break;
            }

        }
    }
}
